package com.ray.service;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.HalfMaterialBean;
import com.ray.bean.MaterialTreeBean;
import com.ray.bean.MaterialTreeViewBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.StructureTreeMapper;
import com.ray.util.ReturnStringUtil;
import com.ray.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StructureTreeService {
	@Autowired
	StructureTreeMapper structureTreeMapper;
	

	/**
	 * 获取物料配置结构树
	 * @isUrl true
	 * @author nate
	 * @param request
	 * @param start(分页参数)
	 * @param length(分页参数)
	 * @return
	 * @throws ServiceException
	 */
	public DatatablesViewPageBean<MaterialTreeBean> getMaterialTreeData(Map map) {
		DatatablesViewPageBean<MaterialTreeBean> datatablesViewPageBean = new DatatablesViewPageBean<MaterialTreeBean>();
		int count = structureTreeMapper.getMaterialTreeDataCount(map);
		datatablesViewPageBean.setiTotalDisplayRecords(count);
		datatablesViewPageBean.setiTotalRecords(count);
		datatablesViewPageBean.setReturnData(structureTreeMapper.getMaterialTreeData(map));
		return datatablesViewPageBean;
	}

	public List<MaterialTreeBean> getCompanyMaterialTree(Map map){
		return structureTreeMapper.getMaterialTreeData(map);
	}

 	/**
	 * 获取物料配置结构树
	 * @isUrl true
	 * @author nate
	 * @param request
	 * @param type(类型 1 整架 2 整框 3 整腿)
	 * @param materialType(加工对象)
	 * @param isReport(是否需要报工)
	 * @param isPurchase(是否需要采购)
	 * @param secondIsShow(整框整腿是否显示)
	 * @param allIsReport(整架是否显示)
	 * @param createTime(创建时间)
	 * @param id(id)
	 * @return
	 * @throws ServiceException 
	 */
	public String  addMaterialTreeData(Map map) {
//		Assert.notNull(map.get("type"),"未获取到类型");
		Assert.notNull(map.get("materialType"),"未获取到加工对象");
		Assert.notNull(map.get("isReport"),"未获取是否报工");
		Assert.notNull(map.get("isPurchase"),"未获取到是否采购");
//		Assert.notNull(map.get("secondIsShow"),"未获取到框腿显示");
		Assert.notNull(map.get("allIsReport"),"未获取到整架显示");
		MaterialTreeBean materialTreeBean =getMaterialTreeByMap(map);
		Assert.isTrue(materialTreeBean== null,"同一个物料类型不能重复添加");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
		map.put("createTime", sdf.format(new Date()));
		map.put("id", StringUtil.getUUID());
		structureTreeMapper.addMaterialTreeData(map);
		return ReturnStringUtil.ADD_SUCCESS;
	}

	/**
	 * @isUrl true
	 * 校验是否存在
	 * @param materialType
	 * @param loginCompany
	 * @param id
	 * @param map
	 */
	public MaterialTreeBean getMaterialTreeByMap(Map map) {
		return structureTreeMapper.getMaterialTreeByMap(map);
	}


	/**
	 * 删除物料配置结构树
	 * @isUrl true 
	 * @author nate
	 * @param request
	 * @param id(id)
	 * @return
	 * @throws ServiceException
	 */
	public String deleteMaterialTreeData(Map map) {
		Assert.notNull(map.get("id"),"未获取到id");
		structureTreeMapper.deleteMaterialTreeData(map);
		return ReturnStringUtil.DELETE_SUCCESS;
	}


	/**
	 * 修改物料配置结构树
	 * @isUrl true
	 * @author nate
	 * @param request
	 * @param id
	 * @param isReport(是否需要报工)
	 * @param isPurchase(是否需要采购)
	 * @param secondIsShow(整框整腿是否显示)
	 * @param allIsReport(整架是否显示)
	 * @return
	 * @throws ServiceException
	 */
	public String updateMaterialTreeData(Map map) {
		Assert.notNull(map.get("id"),"未获取到id");
		Assert.notNull(map.get("isReport"),"未获取是否报工");
		Assert.notNull(map.get("isPurchase"),"未获取到是否采购");
		Assert.notNull(map.get("secondIsShow"),"未获取到框腿显示");
		Assert.notNull(map.get("allIsReport"),"未获取到整架显示");
		structureTreeMapper.updateMaterialTreeData(map);
		return ReturnStringUtil.UPDATE_SUCCESS;
	}


	/**
	 * @isUrl true 
	 * 获取公司半成品组合名称
	 * loginCompany   登录公司
	 * @param map
	 * @return
	 */
	public  DatatablesViewPageBean<HalfMaterialBean> getCompanyHalfMaterialName(Map map){
		DatatablesViewPageBean<HalfMaterialBean> datatablesViewPageBean =new DatatablesViewPageBean<HalfMaterialBean>();
		List<HalfMaterialBean> list=structureTreeMapper.getCompanyHalfMaterialName(map);
		int count=structureTreeMapper.getCompanyHalfMaterialNameCount(map);
		datatablesViewPageBean.setReturnData(list);
		datatablesViewPageBean.setiTotalDisplayRecords(count);
		datatablesViewPageBean.setiTotalRecords(count);
		return datatablesViewPageBean;
	}

	/**
	 * @isurl true
	 * 添加半成品配置
	 * halfMaterialName  半成品配置名称
	 * @param requestParamMap
	 */
	public String addCompanyHalfMaterial(Map<String, Object> map) {
		 StringUtil.checkIsTrue(map, "halfMaterialName", "未获取到物料半成品名称");
		 Assert.isTrue(this.checkCompanyHalfMaterial(map),"该物料名称已存在");
		 int row=structureTreeMapper.addCompanyHalfMaterial(map);
		 Assert.isTrue(row>0,ReturnStringUtil.ADD_ERROR);
		 return ReturnStringUtil.ADD_SUCCESS;
	}

	/**
	 * 校验半成品名称是否存在，存在返回fasel  不存在返回true
	 * @param map
	 * @return
	 */
	private boolean checkCompanyHalfMaterial(Map<String, Object> map) {
		boolean bool=true;
		List<HalfMaterialBean> list=structureTreeMapper.getCompanyHalfMaterialName(map);
		if(null!= list && list.size()>0) {
			bool=false;
		}
		return bool;
	}
	
	/**
	 * @isUrl true
	 * 修改半成品组合名称
	 * id id
	 * halfMaterialName 半成品名称
	 * status 状态
	 * @param map
	 */
	public String updateHalfMaterialName(Map map) {
		StringUtil.checkIsTrue(map, "id", "未获取到id");
		StringUtil.checkIsTrue(map, "halfMaterialName", "未获取到半成品名称");
		StringUtil.checkIsTrue(map, "status", "未获取到状态");
		int row=structureTreeMapper.updateHalfMaterialName(map);
		Assert.isTrue(row>0,ReturnStringUtil.UPDATE_ERROR);
		return ReturnStringUtil.UPDATE_SUCCESS;
	}

	
	/**
	 * @isUrl true
	 * 获取物料半架名称
	 * loginCompany
	 * @param map
	 * @return
	 */
	public List<HalfMaterialBean> getHalfMaterialNameList(Map map){
		return structureTreeMapper.getHalfMaterialNameList(map);
	}
	
	
	/**
	 * @isUrl true
	 * 添加半架组合物料
	 * loginCompany
	 * materialType(物料类型)
	 * nameId(半架名称id)
	 * isShow(报工是否显示)
	 * @param map
	 * @return
	 */
	public String addHalfMaterialType(Map map){
		StringUtil.checkIsTrue(map, "nameId", "未获取到半架名称");
		StringUtil.checkIsTrue(map, "isShow", "未获取到报工是否显示");
		StringUtil.checkIsTrue(map, "materialType", "未获取到物料类型");
		Assert.isTrue(this.checkHalfMaterialType(map),"该半架已含有改物料");
		int row=structureTreeMapper.addHalfMaterialType(map);
		Assert.isTrue(row>0,ReturnStringUtil.ADD_ERROR);
		return ReturnStringUtil.ADD_SUCCESS;
	}
	
	
	/**
	 * @isUrl true
	 * 添加半架组合物料
	 * loginCompany
	 * id
	 * isShow(报工是否显示)
	 * @param map
	 * @return
	 */
	public String updateHalfMaterialType(Map map){
		StringUtil.checkIsTrue(map, "id", "未获取到id");
		StringUtil.checkIsTrue(map, "isShow", "未获取到报工是否显示");
		int row=structureTreeMapper.updateHalfMaterialType(map);
		Assert.isTrue(row>0,ReturnStringUtil.UPDATE_ERROR);
		return ReturnStringUtil.UPDATE_SUCCESS;
	}
	
	
	/**
	 * @isUrl true
	 * 删除半架组合物料
	 * loginCompany
	 * id
	 * @param map
	 * @return
	 */
	public String deleteHalfMaterialType(Map map){
		StringUtil.checkIsTrue(map, "id", "未获取到id");
		int row=structureTreeMapper.deleteHalfMaterialType(map);
		Assert.isTrue(row>0,ReturnStringUtil.DELETE_ERROR);
		return ReturnStringUtil.DELETE_SUCCESS;
	}

	
	/**
	 * 校验半架是否包含物料类型
	 * 不存在返回true 存在返回false
	 * @param map
	 * @return
	 */
	private boolean checkHalfMaterialType(Map map) {
		boolean bool=true;
		List<HalfMaterialBean> materialTyteList=structureTreeMapper.getHalfMaterialType(map);
		if(materialTyteList!=null && materialTyteList.size()>0) {
			bool=false;
		}
		return bool;
	}
	
	/**
	 * @isUrl true
	 * 获取半架物料类型
	 * materialType(物料类型)
	 * nameId(半架名称id)
	 * isShow(报工是否显示)
	 * @param map
	 * @return
	 */
	public DatatablesViewPageBean<HalfMaterialBean> getHalfMaterialType(Map map){
		DatatablesViewPageBean<HalfMaterialBean> datatablesViewPageBean=new DatatablesViewPageBean<HalfMaterialBean>();
		List<HalfMaterialBean> list=structureTreeMapper.getHalfMaterialType(map);
		int count=structureTreeMapper.getHalfMaterialTypeCount(map);
		datatablesViewPageBean.setReturnData(list);
		datatablesViewPageBean.setiTotalRecords(count);
		datatablesViewPageBean.setiTotalDisplayRecords(count);
		return datatablesViewPageBean;
	}
	
	/**
	 * @isUrl true
	 * 获取所有类型
	 * @param map
	 * @return
	 */
	public List<HalfMaterialBean> getHalfMaterialTypeList(Map map){
		return structureTreeMapper.getHalfMaterialType(map);
	}
	
	/**
	 * @isUrl true
	 * 获取物料自动计算的组合方式
	 * @param map
	 * @return
	 */
	public  List<HalfMaterialBean> getAutoOrderMaterialGroup(Map map) {
		return structureTreeMapper.getAutoOrderMaterialGroup(map);
	}
}
